const eventModel = require('../models/events.js')

class EventCtrl {

    async list() {
        let lst = await eventModel.find().sort('eventId');
        return lst.map(m=>m.toJSON());
    }

    async create(eventData) {
        let E = new eventModel(eventData);
        E = await E.save();
        return E.toJSON();
    }

    async retrieve(id) {
        let E = await eventModel.findById(id);
        return E?E.map(e=>e.toJSON()): null;
    }

    async upsert(id, eventData) {
        eventData.id = id;
        let E = await eventModel.upsert(id, eventData);
        return E.toJSON();
    }
    async removeById(id) {
        await eventModel.removeById(id);
        return 1;
    }

    async find(filter)
    {
        let r = await eventModel.find(filter);
        return r.map(e=>e.toJSON());
    }

    async findtime(filter)
    {
        let r = await eventModel.find(filter);//.sort({eventSec: 'asc'});
        return r.map(e=>e.toJSON());
    }
    async duelStats(mid, pid) {
        let total = eventModel.aggregate([{$match:{matchId:mid, playerId:pid, eventId:1}}]);
        let totalgroup = await total.group({_id:"$duelAgainst", totals: {$sum:1}});
        let won = eventModel.aggregate([{$match:{matchId:mid, playerId:pid, eventId:1, eventResults:"Won"}}]);
        let wongroup = await won.group({_id:"$duelAgainst", wons: {$sum:1}});
        let lost = eventModel.aggregate([{$match:{matchId:mid, playerId:pid, eventId:1, eventResults:"Lost"}}]);
        let lostgroup = await lost.group({_id:"$duelAgainst", losts: {$sum:1}});
        let neutral = eventModel.aggregate([{$match:{matchId:mid, playerId:pid, eventId:1, eventResults:"Neutral"}}]);
        let neutralgroup = await neutral.group({_id:"$duelAgainst", neutrals: {$sum:1}});

        //console.log(r)

        let ret = {};
        for( let tg of totalgroup){
            let playerId = tg._id;
            if(!Object.hasOwnProperty.call(ret, playerId)){
                ret[playerId] = { playerId, wons: 0, losts: 0, neutrals: 0, totals: tg.totals};
            }else{
                ret[playerId].totals = tg.totals; 
            }
        }

        for( let wg of wongroup) {
            let playerId = wg._id;
            if(!Object.hasOwnProperty.call(ret, playerId)){
                ret[playerId] = { playerId, wons: wg.wons, losts: 0, neutrals: 0, totals: 0};
            }else{
                ret[playerId].wons = wg.wons; 
            }
        }
        for( let lg of lostgroup) {
            let playerId = lg._id;
            if(!Object.hasOwnProperty.call(ret, playerId)){
                ret[playerId] = { playerId, wons: 0, losts: lg.losts, neutrals: 0, totals: 0};
            }else{
                ret[playerId].losts = lg.losts; 
            }
        }
        for( let ng of neutralgroup) {
            let playerId = ng._id;
            if(!Object.hasOwnProperty.call(ret, playerId)){
                ret[playerId] = { playerId, wons: 0, losts: 0, neutrals: ng.neutrals, totals: 0};
            }else{
                ret[playerId].neutrals = ng.neutrals; 
            }
        }
        return ret
        /**let result=[];
        for(let d of ret) { result.push(d.toJSON())}
        return result*/
    }
    async passStats(mid, pid) {
        let from = eventModel.aggregate([{$match:{matchId:mid, playerId:pid, eventId:8}}]);
        let fromgroup = await from.group({_id:"$passFrom", froms: {$sum:1}});//, fxg:{$avg:"$startX"}, fyg:{$avg:"$startY"}});
        let to = eventModel.aggregate([{$match:{matchId:mid, playerId:pid, eventId:8}}]);
        let togroup = await to.group({_id:"$passTo", tos: {$sum:1}})//, txg:{$avg:"$endX"}, tyg:{$avg:"$endY"}});//moogoose 聚合:累加+平均

        //console.log(r)

        let ret = {};
        for( let fg of fromgroup){
            if (fg._id != 'none'){
                let playerId = fg._id;
                if(!Object.hasOwnProperty.call(ret, playerId)){//ret无playerId,tui
                    ret[playerId] = { playerId, froms: fg.froms, tos: 0}//, fxg: fg.fxg, fyg: fg.fyg, txg: 0, tyg: 0};本来用来计算按传出和传给的球员算平均坐标
                }else{//ret有playerId,tui 对应数值
                    ret[playerId].froms = fg.froms; 
                    //ret[playerId].fxg = fg.fxg;
                    //ret[playerId].fyg = fg.fyg;
                }
            }
            else{continue;}
        }

        for( let tg of togroup) {//另外de聚合
            if (tg._id != 'none'){    
                let playerId = tg._id;
                if(!Object.hasOwnProperty.call(ret, playerId)){
                    ret[playerId] = { playerId, froms: 0, tos: tg.tos}//, fxg: 0, fyg: 0, txg: tg.txg, tyg: tg.tyg};
                }else{
                    ret[playerId].tos = tg.tos;
                    //ret[playerId].txg = tg.txg;
                    //ret[playerId].tyg = tg.tyg; 
                }
            }
            else{continue;}
        }
        return ret
        /**let result=[];
        for(let d of ret) { result.push(d.toJSON())}
        return result*/
    }
}

module.exports = new EventCtrl();